import com.cherry.mybatis.Emp;
import com.cherry.mybatis.dao.EmpDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class T02 {

    private SqlSession session;
    private EmpDao empDao;

    @Before
    public void start() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        session = sessionFactory.openSession();
        empDao = session.getMapper(EmpDao.class);
    }

    @After
    public void end() {
        session.close();
    }

    @Test
    public void qPageRowBounds() {
        int currentPage = 2;//当前第几页
        int page = 2;//一页多少数量

        //起始索引值计算
        RowBounds rowBounds = new RowBounds((currentPage - 1) * page, page);
        List<Emp> list = session.selectList("com.cherry.mybatis.dao.EmpDao.getUserByRowBounds", null, rowBounds);

        System.out.println(list);
    }

    @Test
    public void qPageLimit() {
        List<Emp> list = empDao.findPage(1, 2);
        System.out.println(list);
    }

    @Test
    public void qById() throws IOException {
        Emp emp = empDao.findById(5L);
        System.out.println(emp);
    }

    @Test
    public void qByIdName() throws IOException {
        Emp emp1 = new Emp();
        emp1.setId(3L);
//        emp1.setName("唐僧");
        Emp emp = empDao.findByIdAndName(emp1);
        System.out.println(emp);
    }

    @Test
    public void qByIds() throws IOException {
        int[] ids = new int[]{2, 3, 4};
        List<Emp> list = empDao.findByIds(ids);
        System.out.println(list);
    }

    @Test
    public void qAll() throws IOException {
//        PageHelper.startPage(1, 2);
        List<Emp> list = empDao.findAll();
        System.out.println(list);

        //包装分页后的结果
//        PageInfo<Emp> empPageInfo = new PageInfo<>(list);
//        System.out.println(empPageInfo);
//        System.out.println("总记录数:" + empPageInfo.getTotal() + "\n总页数:" + empPageInfo.getPages()
//                + "\n第" + empPageInfo.getPageNum() + "页");

    }

}
